package test1.realm;


import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.apache.shiro.util.ThreadContext;
import org.junit.After;
import org.junit.Before;

import com.example.demo.entity.Permission;
import com.example.demo.entity.Role;
import com.example.demo.entity.RoleAndPermission;
import com.example.demo.entity.User;
import com.example.demo.entity.UserAndRole;
import com.example.demo.service.PermissionService;
import com.example.demo.service.RoleAndPermissionService;
import com.example.demo.service.RoleService;
import com.example.demo.service.UserAndRoleService;
import com.example.demo.service.UserService;
import com.example.demo.service.impl.PermissionServiceImpl;
import com.example.demo.service.impl.RoleAndPermissionServiceImpl;
import com.example.demo.service.impl.RoleServiceImpl;
import com.example.demo.service.impl.UserAndRoleServiceImpl;
import com.example.demo.service.impl.UserServiceImpl;

/**
 * 安全框架测试基础类
 * @author Henry.Y
 * @date 2019-05-27
 */
public class BaseTestForRealm {

	protected PermissionService permissionService= new PermissionServiceImpl();
	
	protected RoleService roleService= new RoleServiceImpl();
	
	protected UserService userService= new UserServiceImpl();
	
	protected UserAndRoleService userAndRoleService= new UserAndRoleServiceImpl();
	
	protected RoleAndPermissionService roleAndPermissionService= new RoleAndPermissionServiceImpl();
	
	protected String password= "123";
	
	protected Permission p1;
	
	protected Permission p2;
	
	protected Permission p3;
	
	protected Role r1;
	
	protected Role r2;
	
	protected User u1;
	
	protected User u2;
	
	protected User u3;
	
	protected User u4;
	
	@Before
	public void setUp(){
		
		permissionService.deletePermissionTable(); //清空数据
		userService.deleteUserTable();
		roleService.deleteRoleTable();
		userAndRoleService.deleteUserAndRoleTable();
		roleAndPermissionService.deleteRoleAndPermissionTable();
		
		p1= permissionService.createPermission(new Permission("user:create","新增用户模块",Boolean.TRUE)); //新增权限
		p2= permissionService.createPermission(new Permission("user:update","修改用户模块",Boolean.TRUE));
		p3= permissionService.createPermission(new Permission("menu:create","菜单模块新增",Boolean.TRUE));
		
		r1= roleService.createRole(new Role("admin","管理员",Boolean.TRUE));   //新增角色
		r2= roleService.createRole(new Role("user","用户",Boolean.TRUE));
		
		List<RoleAndPermission> roleAndPermissionList= new ArrayList<RoleAndPermission>();
		
		roleAndPermissionList.add(new RoleAndPermission(r1.getRoleId(),p1.getPermissionId()));
		roleAndPermissionList.add(new RoleAndPermission(r1.getRoleId(),p2.getPermissionId()));
		roleAndPermissionList.add(new RoleAndPermission(r1.getRoleId(),p3.getPermissionId()));
		
		roleAndPermissionList.add(new RoleAndPermission(r2.getRoleId(),p1.getPermissionId()));
		roleAndPermissionList.add(new RoleAndPermission(r2.getRoleId(),p2.getPermissionId()));
		
		roleService.correlatePermission(roleAndPermissionList);    //向角色与权限中间表增添信息
		
		u1= userService.createUser(new User("zhang",password));    //创建角色信息
		u2= userService.createUser(new User("li",password));
		u3= userService.createUser(new User("wu",password));
		u4= new User("wang",password);
		u4.setLocked(Boolean.TRUE);
		userService.createUser(u4);
		
		List<UserAndRole> userAndRoleList= new ArrayList<UserAndRole>();
		userAndRoleList.add(new UserAndRole(u1.getId(),r1.getRoleId()));
		
		userService.correlateRoles(userAndRoleList);               //向用户与角色表增添信息
		
		Factory<org.apache.shiro.mgt.SecurityManager> factory= new IniSecurityManagerFactory("classpath:configuration/shiro-ehcache.ini");
		org.apache.shiro.mgt.SecurityManager securityManager= factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);
	}
	
	@After
	public void tearDown()throws Exception{
		ThreadContext.unbindSubject();       //退出时解除绑定Subject到线程,否则对下次测试造成影响
	}
	
	public void login(String configFile,String username,String password){
		
		Subject subject= SecurityUtils.getSubject();              //得到subject,及创建用户名/密码身份验证Token(即用户身份/凭证)
		UsernamePasswordToken token= new UsernamePasswordToken(username,password);
		subject.login(token);                                     //登陆
	}
	
	protected void login(String username,String password){
		
		Subject subject= SecurityUtils.getSubject();     
		UsernamePasswordToken token= new UsernamePasswordToken(username,password);
		//System.out.println(username);
		//System.out.println(password);
		subject.login(token);
	}
	
	public Subject getSubject(){
		
		return SecurityUtils.getSubject();
	}
}
